﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace TC
{
    public class Sequence : BTNode, IRepeat, IBack
    {
        public List<FlowOutput<FlowNode>> flows;
        private int cur = 0;

        public override void OnCreate()
        {
            base.OnCreate();

            flows.Add(this.AddFlowOutPort<FlowNode>(""));
        }

        public override void Execute(Dispatcher chain)
        {
            base.Execute(chain);

            if (cur < flows.Count)
            {
                chain.Call(flows[cur++]);
            }
            else
            {
                chain.End(0);
            }
        }

        public override void Stop()
        {
            base.Stop();

            cur = 0;
        }

        public void Next(int status)
        {
            if (status == 1)
            {
                chain.End(status);
            }
            else
            {
                //只有返回成功，才会继续执行
                Execute(chain);
            }
        }

        public void Back()
        {
#if UNITY_EDITOR
            Debug.Assert(cur > 0);
#endif

            --cur;
        }
    }
}
